home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Games of Daze
/
Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso
/
x2ftp
/
msdos
/
utils
/
tlayr10
/
spr2til.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-19
|
3KB
|
132 lines
/*
* (c) 1995 NikSoft International
*
* SPR2TIL: Converte un file in formato .SPR in .TIL
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <string.h>
#include <sys\stat.h>
#define IF_NEEDED 0
#define ADD_EXT 1
extern unsigned _stklen= 1024;
extern unsigned _heaplen= 4096;
struct TIL_header {
char sign[16];
unsigned version;
unsigned char tile_x, tile_y;
} Header= {"TILE FILE", 0x0100, 16, 16};
char source_filename[90];
char dest_filename[90];
char source_default_suffix[]= ".SPR";
char dest_default_suffix[]=".TIL";
char *add_suffix(char *dest, char *suffix, unsigned mode)
{
char *index;
if ( !( index= strrchr(dest, '.') ) )
return strcat(dest, suffix);
if (mode)
{
*index= 0;
return strcat(dest, suffix);
};
return dest;
}
int open_file(char *filename, unsigned mode)
{
register int fhandle;
if ( mode & O_CREAT)
{
fhandle= creat(filename, S_IREAD | S_IWRITE);
}
else fhandle= open(filename, mode);
if ( fhandle == -1 )
printf("Can't open: %s\nError: %s", filename, strerror(errno));
else setmode(fhandle, O_BINARY);
return fhandle;
}
int main(unsigned argc, char *argv[])
{
register int dest_handle, source_handle;
unsigned bytes2copy, bytes_converted= 0;
unsigned char *RW_Buffer;
puts("SPR to TIL Converter Utility by gaggi@cs.unibo.it ");
switch ( argc )
{
case 1:
puts("Usage:\n spr2til <source file[.SPR]> [ <target file[.TIL]> ]");
return 0;
case 2:
strcpy(source_filename, argv[1]);
strcpy(dest_filename, argv[1]);
break;
case 3:
strcpy(source_filename, argv[1]);
strcpy(dest_filename, argv[2]);
};
add_suffix(source_filename, source_default_suffix, IF_NEEDED);
add_suffix(dest_filename, dest_default_suffix,
( strcmp(source_filename, dest_filename)? IF_NEEDED : ADD_EXT) );
if ( (source_handle= open_file(source_filename, O_RDONLY)) == -1 )
return -1;
if ( (dest_handle= open_file(dest_filename, O_CREAT)) == -1 )
return -1;
read(source_handle, &Header.tile_x, 2*sizeof(unsigned char) );
write(dest_handle, &Header, sizeof(struct TIL_header) );
bytes2copy= Header.tile_x * Header.tile_y;
if ( !(RW_Buffer= (unsigned char *)malloc(bytes2copy)) )
{
puts("Shortage of memory ... aborting");
close(dest_handle);
close(source_handle);
printf("Unlinking %s\n", dest_filename);
unlink(dest_filename);
return -1;
};
while ( read(source_handle, RW_Buffer, bytes2copy) == bytes2copy )
{
bytes_converted += write(dest_handle, RW_Buffer, bytes2copy);
lseek(source_handle, 2*sizeof(unsigned char), SEEK_CUR);
};
free(RW_Buffer);
close(source_handle);
close(dest_handle);
printf("%s converted into %s\n%05u bytes converted\n", source_filename,
dest_filename, bytes_converted);
return 0;
}